package com.ny.demo524;

import java.sql.SQLException;
import java.util.Scanner;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

public class TestConnection {
    private static final String URL = "jdbc:mysql://localhost:3306/usersDb";
    private static final String USERNAME = "root";
    private static final String PASSWORD = "123456";

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("欢迎来到注册系统");
        System.out.println("请输入用户名");
        String username = scanner.next();
        System.out.println("请输入密码");
        String password = scanner.next();

        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            Connection connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
            String sql = "INSERT INTO users (username, password) VALUES (?, ?)";
            PreparedStatement statement = connection.prepareStatement(sql);
            statement.setString(1, username);
            statement.setString(2, password);
            int rowsAffected = statement.executeUpdate();
            if (rowsAffected > 0) {
                System.out.println("注册成功！");
            } else {
                System.out.println("注册失败");
            }
            statement.close();
            connection.close();
        } catch (ClassNotFoundException e) {
            System.err.println("数据库驱动未找到: " + e.getMessage());
        } catch (SQLException e) {
            System.err.println("数据库操作异常: " + e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}